package com.zsummer.zero.thread;

/**
 * �̰߳�ȫ���ɼ��ԣ������ԣ�ԭ����
 * volatile�ؼ��ֿ��Ա�֤�ɼ��ԣ������ԣ������޷���֤ԭ����
 * ���������δ�����ֳ�volatile���������ԣ������ٸ��£���δ�뵽�ȽϺõ���֤��ʽ
 * 
 * @author zhouyujiang
 *
 */
public class ThreadDemo4 {

	private volatile int count = 0;
	private Object lock = new Object();
	
	public static void main(String[] args) {
		ThreadDemo4 demo4 = new ThreadDemo4();
		stopThread stopThread = demo4.new stopThread();
		myThread myThread = demo4.new myThread();
		stopThread.start();
		myThread.start();
		while(true) {
			if(demo4.count != 0 && demo4.count !=1) {
				System.out.println(demo4.count);
			}else {
				System.out.println(demo4.count);
			}
		}
	}
	
	class stopThread extends Thread{
		@Override
		public void run() {
			while(true) {
				if (count == 0) {
					count++;
				}
			}
		}
	}
	
	class myThread extends Thread{
		@Override
		public void run() {
			while(true) {
				if (count == 1) {
					count--;
				}
			}
		}
	}
}
